import { ref } from 'vue'
import { useAsyncAxios } from '@/utils/asyncFuncStore'
import { useCurrActStroe } from '@/stores/currActStore'
import { useModalStore } from '@/stores/modalStore'
import { storeToRefs } from 'pinia'
import type { MyArticlesData, ErrorData } from '@/types/loginType'
export default function () {
  const articles = ref<MyArticlesData[]>([])
  // 获取文章
  async function getArticlesData() {
    const { currUser } = storeToRefs(useCurrActStroe())
    const { getMyArticles } = useAsyncAxios()
    const { changeData } = useModalStore()
    const { status, data } = await getMyArticles(currUser.value.id)
    if (status === 200) {
      // 修改渲染的数据
      ;(data as MyArticlesData[]).map((article) => {
        article.dateTime = new Date(article.dateTime).toLocaleString()
      })
      articles.value = data as MyArticlesData[]
    } else {
      changeData({ status, data: data as ErrorData })
    }
  }

  return {
    articles,

    getArticlesData
  }
}
